#!/bin/bash
# Copyright (c) 2021-2022 iSoftStone Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

export OUT_DIR=$1
export BUILD_TYPE=$2
export KERNEL_ARCH=$3
export PRODUCT_PATH=$4
export DEVICE_NAME=$5
export KERNEL_VERSION=$6
if [ "$BUILD_TYPE" == "small" ];then
    LINUX_KERNEL_OUT=${OUT_DIR}/kernel/${KERNEL_VERSION}
elif [ "$BUILD_TYPE" == "standard" ];then
    LINUX_KERNEL_OUT=${OUT_DIR}/kernel/src_tmp/${KERNEL_VERSION}
fi
LINUX_KERNEL_OBJ_OUT=${OUT_DIR}/kernel/OBJ/${KERNEL_VERSION}

export OHOS_ROOT_PATH=$(pwd)/../../..
# it needs adaptation for more device target
kernel_image=""
arch_name=""

if [ "$KERNEL_ARCH" == "loongarch64" ];then
    kernel_image="vmlinuz"
    arch_name="loongarch"
fi

export KERNEL_IMAGE=${kernel_image}
LINUX_KERNEL_IMAGE_FILE=${LINUX_KERNEL_OBJ_OUT}/${kernel_image}

if [ "$DEVICE_NAME" == "hispark_phoenix"  ];then
export SDK_SOURCE_DIR=${OHOS_ROOT_PATH}/device/soc/hisilicon/hi3751v350/sdk_linux/source
fi


echo “git version switch to  ecccff1814a02da978b6de06f6383a7d6bc1fddf\n”
echo "git version switch ok \n"

make -f kernel.mk

if [ -f "${LINUX_KERNEL_IMAGE_FILE}" ];then
    echo "Image: ${LINUX_KERNEL_IMAGE_FILE} build success"
else
    echo "Image: ${LINUX_KERNEL_IMAGE_FILE} build failed!!!"
    exit 1
fi



exit 0
